package net.vsame.url2sql.action;

import java.util.LinkedHashMap;
import java.util.Map;

import net.vsame.url2sql.helper.SqlHelper;
import net.vsame.url2sql.sql.Model;

import com.alibaba.fastjson.JSONObject;

/**
 * 基础父类
 * 
 * @author gaolei
 *
 */
public class CommonAction extends BaseAction{

	//返回数据载体
	protected Map<String, Object> datas = new LinkedHashMap<String, Object>();
	protected Model user;
	
	public CommonAction() {
		try {
			user = (Model)context.getSessionVal("user");
		} catch (Exception e) {
			user = null;
		}
	}

	/**
	 * 校验参数
	 * 
	 * @param param
	 * @return
	 */
	protected String getParam(String param) {
			
			if (null == context.getParam(param)) {
				context.putError(-2, " ${" + param + "} must exists");
				return null;
			}
			return context.getParam(param);
	}
	
	/**
	 * 校验session参数
	 * 
	 * @param param
	 * @return
	 */
	protected String getSessionParam(Object param) {
		if (null == param) {
			context.putNoLogin();
			return null;
		}
		return param.toString();
	}
	
	/**
	 * 处理结果
	 * @param obj
	 */
	protected void handleResult(JSONObject obj) {
		context.put("code", obj.get("code"));
		context.put("msg", obj.get("msg"));
	}

	public Map<String, Object> getDatas() {
		return datas;
	}

	public void setDatas(Map<String, Object> datas) {
		this.datas = datas;
	}
	
	/**
	 * 根据成员ID获取身份证号
	 * @return
	 */
	protected String getCA006ByCA001() {
		String CA001 = getParam("CA001");
		String cardId = "";
		if(CA001.startsWith("AAB001")) {
			cardId = CA001.split("_")[1];
		}else{
			
			String queryInfo = "select CA006 from RC03 where CA001 = ?";
			Model model = SqlHelper.queryOne(queryInfo, CA001);
			cardId = model.getString("CA006"); // 身份证号
		}
		return cardId;
	}
	
	protected String getCA003ByCA001() {
		String CA001 = getParam("CA001");
		String CA003 = "";
		if(CA001.startsWith("AAB001")) {
			CA003 = CA001.split("_")[1];
		}else{
			
			String queryInfo = "select CA003 from RC03 where CA001 = ?";
			Model model = SqlHelper.queryOne(queryInfo, CA001);
			CA003 = model.getString("CA003"); // 身份证号
		}
		return CA003;
	}
}
